package com.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.entity.HealthInfo;
import com.web.entity.PageResult;
import com.web.mapper.HealthInfoMapper;
import com.web.service.HealthInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HealthInfoServiceImpl implements HealthInfoService {



    @Autowired
    private HealthInfoMapper healthInfoMapper;


    @Override
    public List<HealthInfo> listByUserIdAndDate(HealthInfo healthInfo) {
        return healthInfoMapper.getUserIdAndDate(healthInfo);
    }

    @Override
    public HealthInfo getByUserIdAndDate(HealthInfo healthInfo) {
        return healthInfoMapper.findByUserIdAndDate(healthInfo);
    }

    @Override
    public PageResult<HealthInfo> page(Integer page, Integer pageSize, HealthInfo healthInfo){
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<HealthInfo> healthInfoList = healthInfoMapper.list(healthInfo);
        //解析查询结果并封装
        Page<HealthInfo> p = (Page<HealthInfo>) healthInfoList;
        return new PageResult<HealthInfo>(p.getTotal(),p.getResult());
    }

    @Override
    public HealthInfo findById(Integer id){
        return healthInfoMapper.getById(id);
    }

    @Override
    public List<HealthInfo> findByUser(Integer userId) {
        return healthInfoMapper.findByUserId(userId);
    }

    /*
    修改
     */
    @Override
    public void update(HealthInfo healthInfo){
        healthInfoMapper.update(healthInfo);
    }

    @Override
    public void insert(HealthInfo healthInfo){
        healthInfo.setCreateTime(LocalDateTime.now());
        healthInfo.setUpdateTime(LocalDateTime.now());
        healthInfoMapper.insert(healthInfo);
    }

    @Override
    public void delete(Integer id){
        healthInfoMapper.delete(id);
    }

}
